package com.study.algorithm.base.graph;

import com.study.algorithm.base.graph.model.Graph;
import com.study.algorithm.base.graph.model.GraphEdge;
import com.study.algorithm.base.graph.model.GraphNode;

/**
 * create graph
 *
 * @author wanghuilong
 * @version 1.0
 * @date 2022/8/4 9:06 下午
 */
public class GraphGenerator {

    /**
     * matrix: all edges of the graph
     * n*3 matrix
     * @param matrix [the value of the from node, the value of the to node,weight]
     * @return
     */
    public static Graph createGraph(Integer[][] matrix) {
        Graph graph = new Graph();
        for (int i = 0; i < matrix.length; i++) {
            Integer from = matrix[i][0];
            Integer to = matrix[i][1];
            Integer weight = matrix[i][2];
            if(!graph.nodes.containsKey(from)) {
                graph.nodes.put(from,new GraphNode(from));
            }
            if(!graph.nodes.containsKey(to)) {
                graph.nodes.put(to,new GraphNode(to));
            }
            GraphNode fromGraphNode = graph.nodes.get(from);
            GraphNode toGraphNode = graph.nodes.get(to);
            GraphEdge graphEdge = new GraphEdge(weight, fromGraphNode, toGraphNode);
            fromGraphNode.nexts.add(toGraphNode);
            fromGraphNode.out++;
            toGraphNode.in++;
            fromGraphNode.edges.add(graphEdge);
            graph.edges.add(graphEdge);
        }
        return graph;
    }

    public static void main(String[] args) {
        System.out.println(814205930438736L >> 23);
        System.out.println((814205930438736L >> 23) % 128);
    }
}
